<!DOCTYPE html>
<html>

<head>
    <title>Store Admin Page</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>

<body>
    <header>
        <h2>UIC Second-Hand Store Admin page</h2>
    </header>
    <main>
        <div class="content">
            <h3>
                <?php
                session_start();
                $user = $_SESSION['user'];
                echo "You are currently login as admin, username: $user";
                ?>
                <button type="button" class="styledBtn" onclick="window.location.href='LoginPage.html'">Logout</button>
            </h3>
        </div>
        <div class="content">
            <h2>Item management</h2>
            <hr>
            <!--form to add items-->
            <form action="addItem.php" method="POST">
                <!--table to display items-->
                <table border='1' class="admintable">
                    <?php
                    include "connectStoreDB.php";

                    $sql = "SELECT * FROM inventory;";

                    $result = mysqli_query($conn, $sql);

                    //check if result is empty
                    if (mysqli_num_rows($result) > 0) {

                        //print the heading of table
                        //echo "<table border='1'>";
                        echo "<tr>";

                        //print each column name
                        while ($property = mysqli_fetch_field($result)) {
                            echo "<th>" . $property->name . "</th>";
                        }
                        echo "<th>Action</th></tr>";

                        //get number of columns
                        $fields = mysqli_num_fields($result);

                        //Get each row data
                        while ($row = mysqli_fetch_array($result)) {
                            echo "<tr>";
                            for ($f = 0; $f < $fields; $f++) {
                                echo "<td>" . $row[$f] . "</td>";
                            }
                            echo "<td><button type=\"button\" onclick=\"deleteItem('" . $row[0] . "')\">Delete this item</button></td></tr>";
                        }

                        //echo "</table>";
                        //echo mysqli_num_rows($result)." results";
                    } else {
                        echo "0 results";
                    }

                    //close connection
                    mysqli_close($conn);
                    ?>
                    <tr>

                        <td><input type="text" name="item" placeholder="Input item name" required></td>
                        <td><input type="text" name="type" placeholder="Input classification" required></td>
                        <td><input type="text" name="number" placeholder="Input amount" required></td>
                        <td><input type="text" name="sell_p" placeholder="Input selling price" required></td>
                        <td><input type="text" name="buy_p" placeholder="Input buying price" required></td>
                        <td class="flexDisplay"><button type="submit">Add item</button></td>

                    </tr>
                </table>
            </form>
        </div>
    </main>

    <script>
        function deleteItem(item) {
            if (confirm("Are you sure to delete " + item + "?")) {
                location.href = "delItem.php?item='" + item + "'";
            }
        }
    </script>
</body>

</html>